package com.tsystems.tuni.sportplanet.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.xml.bind.annotation.XmlTransient;

/**
 * Game Entity.
 * 
 * @author Yuriy Eremin
 * 
 */
@Entity
@Table(name = "game", catalog = "sportsplanet")
public class Game implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Integer gameId;
	private Season season;
	private Sport sport;
	private Location location;
	private Date time;
	private Set<Result> results = new HashSet<Result>(0);

	public Game() {
	}

	public Game(Season season, Sport sport, Location location) {
		this.season = season;
		this.sport = sport;
		this.location = location;
	}

	public Game(Season season, Sport sport, Location location, Date time,
			Set<Result> results) {
		this.season = season;
		this.sport = sport;
		this.location = location;
		this.time = time;
		this.results = results;
	}

	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "game_id", unique = true, nullable = false)
	// @XmlID
	// @XmlJavaTypeAdapter(value = IntegerAdapter.class, type = String.class)
	public Integer getGameId() {
		return this.gameId;
	}

	public void setGameId(Integer gameId) {
		this.gameId = gameId;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "SEASON_season_id", nullable = false)
	@XmlTransient
	public Season getSeason() {
		return this.season;
	}

	public void setSeason(Season season) {
		this.season = season;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "SPORT_sport_id", nullable = false)
	@XmlTransient
	public Sport getSport() {
		return this.sport;
	}

	public void setSport(Sport sport) {
		this.sport = sport;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "LOCATION_location_id", nullable = false)
	// @XmlIDREF
	@XmlTransient
	public Location getLocation() {
		return this.location;
	}

	public void setLocation(Location location) {
		this.location = location;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "time", length = 19)
	public Date getTime() {
		return this.time;
	}

	public void setTime(Date time) {
		this.time = time;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "game")
	@XmlTransient
	public Set<Result> getResults() {
		return this.results;
	}

	public void setResults(Set<Result> results) {
		this.results = results;
	}

	@Override
	public String toString() {
		return "com.tsystems.tuni.sportplanet.entity.Game[id=" + gameId + "]";
	}

	// @Override
	// public Object onCycleDetected(Context arg0) {
	// System.out.println("CycleRecoverable.onCycleDetected # ".concat(this
	// .toString()));
	// Game game = new Game();
	// game.setGameId(this.getGameId());
	// return game;
	// }

}
